package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IStrategyCommentRepository;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {


    @Autowired
    private IStrategyCommentRepository strategyCommentRepository;


    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 添加攻略评论实现
     * @param strategyComment
     */
    @Override
    public void save(StrategyComment strategyComment) {
         if(!StringUtils.hasLength(strategyComment.getId())){
            strategyComment.setCreateTime(new Date());
         }
        strategyCommentRepository.save(strategyComment);
    }

    /**
     * 评论列表+分页
     * @param qo
     * @return
     */
    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {

        //repository 和 mongodbtemplate 都不能直接得到page对象

        //拼接条件
        Query query = new Query();
        if(qo.getDetailId()!=-1){
            Criteria criteria = Criteria.where("detailId").is(qo.getDetailId());
            query.addCriteria(criteria);
        }
        //获取总数
        long total = mongoTemplate.count(query, StrategyComment.class);
        if(total == 0){
            return Page.empty();
        }
        //分页
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.DESC, "createTime");
        List<StrategyComment> commentList = mongoTemplate.find(query, StrategyComment.class);

        return new PageImpl<>(commentList,pageable,total);
    }

    /**
     * 点赞
     * @param id
     * @param userId
     */
    @Override
    public void commentThumbUp(String id, Long userId) {
        StrategyComment strategyComment = strategyCommentRepository.findById(id).get();

        List<Long> thumbuplist = strategyComment.getThumbuplist();
        //当请求过来时,需要判断点赞用户id集合是否包含当前登录用户id
        if(!thumbuplist.contains(userId)){
            //如果当前用户id不在集合中,表示点赞操作,点赞数+1,将用户id加入集合中
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
            thumbuplist.add(userId);
        }else {
            //如果当前用户id在集合中,表示取消点赞操作,点赞数-1,将用户id从集合中移除
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
            thumbuplist.remove(userId);
        }
        //最后进行保存操作
        strategyCommentRepository.save(strategyComment);
    }
}
